package com.iretailer.report.web.business;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iretailer.report.base.BaseAction;
import com.iretailer.report.dto.ReRecover;
import com.iretailer.report.model.Malls;
import com.iretailer.report.service.CollectService;
import com.iretailer.report.util.RMap;
import com.iretailer.report.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xjw
 * @Date: 2019/3/12 15:15
 * @Description:
 */
@RestController
public class RecoverController extends BaseAction {
    @Autowired
    private CollectService collectService;

    @RequestMapping("/business/recoverHistory.action")
    public Map<String, Object> recoverHistory(@RequestBody ReRecover recover) {
        List<String> dates = TimeUtil.getBetweenDays(recover.getStartDate(), recover.getEndDate());
        List<Malls> malls = new ArrayList<>();
        if (recover.getMallId().equals("All")) {
            malls = mallsService.list(new QueryWrapper<Malls>().lambda().eq(Malls::getEnabled, 1));
        } else {
            malls.add(mallsService.getById(recover.getMallId()));
        }
        collectService.collect(malls, dates);
        return RMap.ok();
    }

    @RequestMapping("/business/resetBusinessTime.action")
    public Map<String, Object> resetBusinessTime(@RequestBody ReRecover recover) {
        try {
            List<String> dates = TimeUtil.getBetweenDays(recover.getStartDate(), recover.getEndDate());
            List<Malls> malls = new ArrayList<>();
            if (recover.getMallId().equals("All")) {
                malls = mallsService.list(new QueryWrapper<Malls>().lambda().eq(Malls::getEnabled, 1));
            } else {
                malls.add(mallsService.getById(recover.getMallId()));
            }

            collectService.resetBusiness(malls, dates);
            return RMap.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return RMap.error();
        }
    }
}
